package com.cloud.wms.ui.common.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.os.Parcelable;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.base.BasePresenter;
import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.core.widget.decoration.LinearVerticalDecoration;
import com.cloud.widget.utils.DensityUtils;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.in.contract.InShelfContract;
import com.cloud.wms.ui.in.entity.InCommodityEntity;
import com.cloud.wms.ui.in.entity.InOrderEntity;
import com.cloud.wms.ui.in.entity.ShelfBatchEntity;
import com.cloud.wms.ui.in.presnter.InShelfPresenter;
import com.cloud.wms.ui.main.entity.BatchEntity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-26 15:28
 * @Description:
 */
public class SelectBatchActivity extends BaseBarListActivity<BatchEntity, BasePresenter> {
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private boolean isSingle;
    private ArrayList<BatchEntity> batchList;

    @Override
    protected String barTitle() {
        return "选择批次";
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_common_select_batch;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        btnSubmit.setEnabled(false);
        batchList = getIntent().getParcelableArrayListExtra(IntentKey.LIST_KEY);
        isSingle = getIntent().getBooleanExtra(IntentKey.TYPE_KEY, false);
        mAdapter.setNewInstance(batchList);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, BatchEntity item) {
        helper.setText(R.id.tv_produce_date, item.getProductionDate())
                .setText(R.id.tv_deadline_date, item.getExpirationDate())
                .setText(R.id.tv_in_date, item.getInDate())
                .setTextColorRes(R.id.tv_produce_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_produce_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setTextColorRes(R.id.tv_deadline_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_deadline_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setTextColorRes(R.id.tv_in_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_in_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setBackgroundResource(R.id.ll_item_content, item.isSelect() ? R.drawable.bg_item_common_select : R.drawable.bg_item_common);
    }

    @Override
    protected int getResId() {
        return R.layout.activity_common_select_batch;
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<BatchEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        if (isSingle) {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                BatchEntity entity = mAdapter.getData().get(i);
                if (i == position) {
                    entity.setSelect(!entity.isSelect());
                } else {
                    entity.setSelect(false);
                }
            }
        } else {
            mAdapter.getData().get(position).setSelect(!mAdapter.getData().get(position).isSelect());
        }
        mAdapter.notifyDataSetChanged();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            BatchEntity entity = mAdapter.getData().get(i);
            if (entity.isSelect()) {
                btnSubmit.setEnabled(true);
                return;
            }
        }
        btnSubmit.setEnabled(false);
    }

    @Override
    protected RecyclerView.ItemDecoration itemDecoration() {
        return new LinearVerticalDecoration(DensityUtils.Dp2Px(this, 10));
    }

    @Override
    protected void setupComponent() {
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        ArrayList<BatchEntity> entities = new ArrayList<>();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            BatchEntity entity = mAdapter.getData().get(i);
            if (entity.isSelect()) {
                entities.add(entity);
            }
        }
        if (entities.isEmpty()) {
            info("请选择批次后提交");
            return;
        }
        if (isSingle) {
            EventBusUtil.sendEvent(new Event<>(EventConfig.SELECT_BATCH_SINGLE, entities.get(0)));
        } else {
            EventBusUtil.sendEvent(new Event<>(EventConfig.SELECT_BATCH, entities));
        }
        this.finish();
    }

    public static void start(Context context, ArrayList<BatchEntity> entities, boolean isSingle) {
        Intent intent = new Intent(context, SelectBatchActivity.class);
        intent.putExtra(IntentKey.LIST_KEY, entities);
        intent.putExtra(IntentKey.TYPE_KEY, isSingle);
        context.startActivity(intent);
    }
}
